﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using Catalyst.Mvc;
using Trirand.Web.Mvc;

namespace Catalyst.Mvc.Extensions
{
	public class LiteContext
	{
		private System.Web.Mvc.HtmlHelper m_Helper;

		public LiteContext(System.Web.Mvc.HtmlHelper helper)
		{
			m_Helper = helper;
		}

		public string RenderLiteGrid<TGrid>(TGrid liteGrid, string id)
			where TGrid : LiteGrid<TGrid>, new()
		{
			liteGrid.CombineGridInitializedFunctions();

			StringBuilder viewText = new StringBuilder();
			viewText.AppendLine(new TrirandNamespace().JQGrid(liteGrid, id).ToString());
			viewText.AppendLine();

			viewText.AppendLine("<script type=\"text/javascript\">");

			if (!String.IsNullOrEmpty(liteGrid.ClientSideOnDocumentReady))
			{
				viewText.AppendLine(LiteEditResources.DocumentReadyScript.Replace("[SCRIPT]", liteGrid.ClientSideOnDocumentReady));
			}

			// edit script
			if (m_Helper.ViewData["HasScript"] == null)
			{
				viewText.AppendLine(LiteEditResources.EditScript);
				viewText.AppendLine();

				viewText.Append(LiteEditResources.ColumnDependencyScript);
				viewText.AppendLine();

				m_Helper.ViewData.Add("HasScript", true);
			}


			//if (liteGrid.HierarchySettings.HierarchyMode == HierarchyMode.Parent
			//    || liteGrid.HierarchySettings.HierarchyMode == HierarchyMode.None)
			//{
			//    // add new button script
			//    viewText.AppendLine(LiteEditResources.AddNewButtonScript.Replace("[GridId]", id));
			//    viewText.AppendLine();
			//}

			viewText.AppendLine("</script>");
			viewText.AppendLine();

			return viewText.ToString();
		}

		public string AutoComplete(string id, string dataUrl, AutoCompleteMode autoCompleteMode)
		{
			return m_Helper.Trirand().JQAutoComplete(new JQAutoComplete
				{
					DisplayMode = AutoCompleteDisplayMode.ControlEditor,
					AutoCompleteMode = autoCompleteMode,
					DataUrl = dataUrl
				}, id).ToString();
		}
	}
}